
let isOpenSocket = false//是否已经连接上ws
let heartBeatDelay = 30000//心跳间隔，单位毫秒
let heartBeatInterval = null
let heartBeatText = {"type":"ping"}//心跳时发送的消息文本
let reconnectTimes = 10//最大重连次数
let reconnectInterval = null
let reconnectDelay = 3000//重连间隔，单位毫秒
//let wsUrl = "wss://tatatest.dog8.cc/wss"
//let wsUrl = "ws://bendi.tatatest.dog8.cc/wss"
let wsUrl = "wss://service.meowsc.com/wss"
let socketTask = null
//这个参数是防止重连失败之后onClose方法会重复执行reconnect方法，导致重连定时器出问题
//连接并打开之后可重连，且只执行重连方法一次
let canReconnect = false
//封装的对象，最后以模块化向外暴露，
//init方法 初始化socketTask对象
//completeClose方法 完全将socketTask关闭（不重连）
//其他关于socketTask的方法与uniapp的socketTask api一致



let ws = {
	socketTask: null,
	init,
	completeClose
}

function init(uid) {
	if(uid == ''){
		console.log('no uid can not connect websocket');
		return;
	}
	socketTask = new WebSocket(wsUrl)
	socketTask.onopen = () => {
		console.log("ws连接成功")
		clearInterval(heartBeatInterval)
		clearInterval(reconnectInterval)
		isOpenSocket = true
		canReconnect = true
		//开启心跳机制
		heartBeat()
		
		bindUid(uid)
	}
	socketTask.onmessage = (res) => {
		//自定义处理onMessage方法
		//console.log(res)
	}
	socketTask.onclose = () => {
		if(isOpenSocket){
			console.log("ws与服务器断开")
		}else{
			console.log("连接失败")
		}
		isOpenSocket = false
		if(canReconnect){
			reconnect()
			canReconnect = false
		}
	}
	ws.socketTask = socketTask
}


function heartBeat() {
	heartBeatInterval = setInterval(() => {
		//console.log(heartBeatText)
		send( JSON.stringify(heartBeatText) );
	}, heartBeatDelay)
}
function bindUid(uid){
	let data = {type:'bindUser',uid:uid}
	console.log(data)
	send(JSON.stringify(data))
	


	
}
function send(value) {

	ws.socketTask.send(value);
}

function reconnect() {
	//停止发送心跳
	clearInterval(heartBeatInterval)
	//如果不是人为关闭的话，进行重连
	if (!isOpenSocket) {
		let count = 0;
		reconnectInterval = setInterval(() => {
			console.log("正在尝试重连")
			init();
			count++
			//重连一定次数后就不再重连
			if(count >= reconnectTimes){
				clearInterval(reconnectInterval)
				console.log("网络异常或服务器错误")
			}
		}, reconnectDelay)
	}
}
function completeClose(){
	//先将心跳与重连的定时器清除
	clearInterval(heartBeatInterval)
	clearInterval(reconnectInterval)
	canReconnect = false
	ws.socketTask.close()
	ws.socketTask=null
}

module.exports = ws
